package pub.guoxin.oops.common.restful;

import com.google.common.base.Predicates;
import org.springframework.web.context.request.async.DeferredResult;
import pub.guoxin.oops.constants.OopsJwtConstants;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;

/**
 * Standardize API_V1 management using {@link SwaggerConfiguration}
 * <p>
 * extends {@link SwaggerConfiguration} Implementation
 * {@linkplain SwaggerConfiguration#baseDocket(String, String, String, String)}
 * <p>
 * eg:
 *
 * @Configuration
 * @EnableSwagger2 public class ProjectNameSwaggerConfiguration extends SwaggerConfiguration {
 * @Bean public Docket ModelNameApi() {
 * return baseDocket("model-name", "/project-name/api/demo/.*", "中文模块模块名称", "1.0");
 * }
 * }
 * <p>
 * Created by guoxin on 17-10-17.
 */
public abstract class SwaggerConfiguration {

    protected Docket baseDocket(String groupName, String path, String title, String version) {
        //可以添加多个header或参数
        ParameterBuilder builder = new ParameterBuilder();
        builder
                .parameterType("header") //参数类型支持header, cookie, body, query etc
                .name(OopsJwtConstants.HEADER) //参数名
                .defaultValue(OopsJwtConstants.Bearer) //默认值
                .description("header token")
                .modelRef(new ModelRef("string"))//指定参数值的类型
                .required(false).hidden(false).build(); //非必需，这里是全局配置，然而在登陆的时候是不用验证的
        List<Parameter> parameters = new ArrayList<>();
        parameters.add(builder.build());
        return baseDocketNoHeader(groupName, path, title, version).globalOperationParameters(parameters);
    }

    protected Docket baseDocketNoHeader(String groupName, String path, String title, String version) {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName(groupName)
                .genericModelSubstitutes(DeferredResult.class)
                .useDefaultResponseMessages(false)
                .forCodeGeneration(true)
                .pathMapping("/")// base，最终调用接口后会和paths拼接在一起
                .select()
                .paths(Predicates.or(PathSelectors.regex(path + "/.*")))//过滤的接口
                .build()
                .apiInfo(baseApiInfo(title, groupName, version));
    }

    private ApiInfo baseApiInfo(String title, String groupName, String version) {
//        ApiInfo apiInfo = new ApiInfo("Oops " + title + " API_V1",//大标题
//                "Oops " + groupName + "'s REST API_V1, all the applications could access the Object model data via JSON.",//小标题
//                version,//版本
//                "NO terms of service",
//                "liuguoxin@airspace.cn",//作者
//                "The Apache License, Version 2.0",//链接显示文字
//                "http://www.apache.org/licenses/LICENSE-2.0.html"//网站链接
//        );

        return new ApiInfoBuilder()
                .title(title)
                .description("Airspace " + groupName + "'s REST API, all the applications could access the Object model data via JSON.")
                .termsOfServiceUrl("NO terms of service")
                .build();
//        return apiInfo;
    }

    /**
     * new ApiInfoBuilder()
     .title(title)
     .description("Airspace " + groupName + "'s REST API_V1, all the applications could access the Object model data via JSON.")
     .termsOfServiceUrl("NO terms of service")
     .build();
     */
}
